package edu.zut.controller;

import edu.zut.service.UserService;
import edu.zut.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet", value = "/user/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进行前后端交互的操作
        //获取请求参数
        String user_name = request.getParameter("user_name");
        String user_psw = request.getParameter("user_psw");
        //空验证省略
        //验证 调用业务层中的操作
        UserService userService = new UserServiceImpl();
        boolean result = userService.userLogin(user_name.trim(), user_psw.trim());
        if (result){
            //登录成功后，将用户名放入session
            HttpSession session = request.getSession();
            session.setAttribute("user_name",user_name);
//            session.setAttribute("loginStatus",true);
            //资源跳转
//            response.sendRedirect(request.getContextPath() + "/view/home.jsp");
            //跳转到查询全部员工信息
            response.sendRedirect(request.getContextPath() + "/emp/page");
        }else {
            //资源跳转
            request.setAttribute("error","用户名或密码错误");
            request.getRequestDispatcher("/view/login.jsp").forward(request,response);
        }
    }
}
